$(function(){
	$("#enterpriseWinId").window('close');
	$("#updatEenterpriseWinId").window('close');
	
	$('#dgEnterprise').datagrid().datagrid('getPager');
	initBumen();
})


function initBumen(){
	$('#dgDepartment').datagrid({    
	    url:'../../department/queryDepartment',
	    rownumbers:true,
	    singleSelect:true,
	    pagination:true,
	    method:'post',
	    queryParams: {
	    	type:'5',
	    	isDel:'1'
		},
		onClickRow: function (rowIndex, rowData) {
			$('#dgEnterprise').datagrid('loadData',{total:0,rows:[]}); 
            var id = rowData.id; 
            initByType(id);
        }, 
	    columns:[[       
	        {field:'departmentName',title:'部门名称',width:'96%',align:'center'}    
	          
	    ]]    
	});
	  var p1 = $('#dgDepartment').datagrid('getPager');

	   p1.pagination({
	        beforePageText: '',//页数文本框前显示的汉字 
	        afterPageText: '',
	        layout:['first','prev','list','next','last'],
	        displayMsg: '第{from}至{to}条  共{total}条数据',
	    });
}

function initByType(bumenid){
		$('#dgEnterprise').datagrid({    
		    url:'../enterprise/queryEnterprise',
			autoRowHeight:false,
		    rownumbers:true,
		    singleSelect:true,
		    pagination:true,
		    method:'post',
		    queryParams: {
		    	type:bumenid,
		    	isOrder:'y'
			},
		    columns:[[ 
		        {field:'departmentName',title:'部门名称',width:'130',align:'center'},
		        {field:'enterpriseName',title:'自然人或单位名称',width:'140',align:'center'},
		       /* {field:'organizationCode',title:'证件号',width:'180',align:'center'},*/
		        {field:'projectName',title:'失信黑名单项目',width:'120',align:'center'},
		        /*{field:'reason',title:'列入黑名单缘由',width:'180',align:'center'},*/
		        {field:'addDate',title:'列入黑名单日期',width:'120',align:'center'},
		        {field:'validity',title:'有效期',width:'130',align:'center'},
	 	        {
				field : 'isDel',title : '启用/禁用',width : '100',align : 'center',
					formatter : function(value, row, index) {
						if (value == "1") {
							return '<span><a href="#" style="text-decoration:none;color:green"'
									+ 'onclick=qidel("' + row.sysId + '",' + value
									+ '); >已 启 用</a></span>';
						} else if (value == "2") {
							return '<span><a href="#" style="text-decoration:none;color:red" '
									+ 'onclick=qidel("' + row.sysId + '",' + value
									+ '); >已 禁 用</a></span>';
						} else {
							return value;
						}
					}
			}
		        
		    ]]    
		});
	
}

/* 点击搜索时触发 */
function init() {

	var enterpriseName = $("#enterpriseNameMsgId").val().replace(/\s/g, "");//单位名称
	var organizationCode = $("#organizationCodeMsgId").val().replace(/\s/g, "");//组织机构代码
	var projectName = $("#projectNameMsgId").val().replace(/\s/g, "");//项目名称
	var isDel = $("#isDelMsgId").combobox('getValue');//1显示，2禁用

	$('#dgEnterprise').datagrid({
		url : '../enterprise/queryEnterprise?dt=' + new Date().getTime(),
		queryParams : {
			enterpriseName : enterpriseName,
			organizationCode : organizationCode,
			projectName : projectName,
			type:sysType,
			isOrder:'y',
			isDel : isDel
		}

	});
}
/* 点击重置时触发 */
function chongzhi() {
	$('#enterpriseNameMsgId').textbox('clear');
	$('#projectNameMsgId').textbox('clear');
	$('#isDelMsgId').combobox('setValue', '');

}
//启用/删除
function qidel(sysId, value) {

	var isDel = "";
	var delName = "";
	if (value == '1') {
		isDel = '2';
		delName = "禁用";
	}
	if (value == '2') {
		isDel = '1';
		delName = "启用";
	}

	$.messager.confirm('确认', '您确认想要 [ ' + delName + ' ] 数据吗？', function(r) {
		if (r) {
			$.ajax({
				url : '../enterprise/updateEnterprise',
				type : 'post',
				data : {
					"sysId" : sysId,
					"isDel" : isDel
				},
				dataType : 'json',
				error : function() {
					$.messager.alert("提示", delName + "异常", "error");
				},
				success : function(map) {
					var row = map.row;
					if (row >= 0) {
						$.messager.alert("提示", delName + "成功", "info");
						$('#dgEnterprise').datagrid("reload");
						return;
					} else {
						$.messager.alert("提示", delName + "失败", "error");
						return;
					}
				}
			});
		}
	});
}

//修改信息
function updatEenterpriseWin(){ 
	var dgEnterprise = $('#dgEnterprise').datagrid('getSelected');//自然人红黑名单信息
	if(dgEnterprise==null){
		$.messager.alert("提示","请选择一条信息","error");
	    return;
	}
	var sysId = dgEnterprise.sysId;
	
	$("#departmentNameId").textbox('setValue',dgEnterprise.departmentName);
	$("#enterpriseNameId").textbox('setValue',dgEnterprise.enterpriseName);
	$("#organizationCodeId").textbox('setValue',dgEnterprise.organizationCode);
	$("#projectNameId").textbox('setValue',dgEnterprise.projectName);
	$("#reasonId").textbox('setValue',dgEnterprise.reason);
	$('#addDateId').datebox('setValue', dgEnterprise.addDate);	// 设置日期输入框的值
	$('#validityId').datebox('setValue', dgEnterprise.validity);	// 设置有效期日期输入框的值
	$('#updatEenterpriseWinId').dialog({
		title: '修改信息',
		width: 400,
		height: 350,
		closed: false,
		cache: false,
		modal: true,
		buttons:[{
			text:'修改',
			iconCls:'icon-edit',
			handler:function(){
				updatEenterprise(sysId);
			}
		},{
			text:'退出',
			iconCls:'icon-no',
			handler:function(){
				$('#updatEenterpriseWinId').dialog('close');
			}
		}]
	});
} 
//根据sysId修改自然人信息
function updatEenterprise(sysId){

	var enterpriseName = $("#enterpriseNameId").val().replace(/\s/g, "");
	
	if(enterpriseName==''){
		var errorStr='自然人或企业名称不允许为空';
	
		$.messager.alert("提示",errorStr,"error");
	    return;
	}
	
	var departmentName = $("#departmentNameId").val().replace(/\s/g, "");
	var organizationCode = $("#organizationCodeId").val().replace(/\s/g, "");
	var projectName = $("#projectNameId").val().replace(/\s/g, "");
	var reason = $("#reasonId").val().replace(/\s/g, "");
	var addDate = $('#addDateId').datebox('getValue');
	var validity = $('#validityId').datebox('getValue');
	
	$.ajax({  
		url:'../enterprise/updateEnterprise',
		type:'post',
		data:{
		'sysId':sysId,
		'enterpriseName':enterpriseName,
		'organizationCode':organizationCode,
		'projectName':projectName,
		'reason':reason,
		'addDate':addDate,
		'validity':validity,
		'departmentName':departmentName
		
		},
		dataType:'json',
		error:function(){
			$.messager.alert("提示","操作异常","error");
		}, 
		success:function(data){
			var row = data.row;
			if(row>=0){
				$.messager.alert("提示","修改成功","info");
				$('#dgEnterprise').datagrid("reload");
				$("#updatEenterpriseWinId").window('close');
			}else{
				$.messager.alert("提示","修改失败","error");
			}
		}
	})
}

//刷新列表
function refresh(){
	$('#dgEnterprise').datagrid("reload");
}
var nowing='1';
//导入excle弹出窗
function enterpriseWin(){
	var dgDepartment = $('#dgDepartment').datagrid('getSelected');//部门信息
	if(dgDepartment==null){
		$.messager.alert("提示","请先选择部门信息","error");
	    return;
	}
	var bumenid = dgDepartment.id;
	var titleMsg='导入黑名单信息';
    $("#showId").show();
    $("#hideId").hide();
    $("#file_excle").after( $("#file_excle").clone().val(""));  
    $("#file_excle").remove();  
	$('#enterpriseWinId').dialog({
		title: titleMsg,
		width: 330,
		height: 200,
		closed: false,
		cache: false,
		modal: true,
		buttons:[{
			text:'导入信息',
			iconCls:'icon-edit',
			handler:function(){
				var fileVal = $("#file_excle").val();
				if(fileVal ==''){
					alert("请先选择需要解析的EXCLE");
					return;
				}
				if(nowing=='2'){
					alert("正在导入信息，请耐心等待！");
					return;
				}
				nowing='2';
				fn_excle_upload(bumenid);
			}
		},{
			text:'退出',
			iconCls:'icon-no',
			handler:function(){
				$('#enterpriseWinId').dialog('close');
			}
		}]
	});
} 

//上传excle
function fn_excle_upload(bumenid){
	
	$("#showId").hide();
    $("#hideId").show();
	
	  var url = '../upload/fileUpload';
	  var suffixStr = "xlsx,xls";
	  var savePath = ",them,fileload_excle";
	  $.ajaxFileUpload
    ({//String fileObjectId,int MaxSize,String suffixStr,String savePath,int width,int height
           url: url+'?fileObjectId=file_excle&MaxSize='+0+'&suffixStr='+suffixStr+'&savePath='+savePath+'&width='+0+'&height='+0, //用于文件上传的服务器端请求地址
           secureuri: false, //是否需要安全协议，一般设置为false
           fileElementId: 'file_excle', //文件上传域的ID
           dataType: 'json', //返回值类型 一般设置为json
           success: function (data, status)  //服务器成功响应处理函数
           {
               if(data.success=="true"){
             	     var excleUrl = data.imgurl;
             	     
             	    $.ajax({  
            			url:'../enterprise/fileLoadExcle',
            			type:'post',
            			data:{
            			"url":excleUrl,
            			"type":bumenid
            			},
            			dataType:'json',
            			error:function(){
            				$("#showId").show();
        		       	    $("#hideId").hide();
        		       	    nowing='1';
            				$.messager.alert("提示","操作异常","error");
            			}, 
            			success:function(data){
            				nowing='1';
            				var code = data.code;
            				if(code=='300'){
            					$.messager.alert("提示","请检查导入的模板是否正确","error");
            					$("#showId").show();
            		       	    $("#hideId").hide();
            					return;
            				}
            				if(code=='400'){
            					$.messager.alert("提示","自然人或单位名称不允许为空","error");
            					$("#showId").show();
            		       	    $("#hideId").hide();
            					return;
            				}
            				var count = data.count;
            				if(count>=0){
            					$.messager.alert("提示","本次导入"+count+"条数据","info");
            					$('#dgEnterprise').datagrid("reload");
            					$('#enterpriseWinId').dialog('close');
            					
            				}else{
            					$("#showId").show();
            	            	$("#hideId").hide();
            					$.messager.alert("提示","导入数据失败","error");
            					
            				}
            				
            				
            			}
            		})
             	     
               }else{
            	    $("#showId").show();
            	    $("#hideId").hide();
            	    nowing='1';
             	    alert(data.msg);
               }
           },
           error: function (data, status, e)//服务器响应失败处理函数
           {
        	   $("#showId").show();
       	       $("#hideId").hide();
       	       nowing='1';
               alert(e);
           }
    })
}



